<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\model\UserWatch as UserWatchModel;
use app\common\model\WatchBloodOxygen;
use app\common\model\WatchBloodPressure;
use app\common\model\WatchHeartRate;
use app\common\model\WatchSetting;
use app\common\model\WatchTurnover;
use fast\Date;
use think\Config;

/**
 * 手表设置
 */
class UserWatch extends Api
{
//    protected $noNeedLogin = ['bind','unbind','last'];
    protected $noNeedRight = '*';

    public function _initialize()
    {
        parent::_initialize();

        if (!Config::get('fastadmin.usercenter')) {
            $this->error(__('User center already closed'));
        }

    }

    //我的设备列表
    public function bindlist(){
        $user_id = $this->auth->id;
        $UserWatchModel = new UserWatchModel();
        $data  =$UserWatchModel
            ->alias('uw')
            ->join('watch_setting ws','uw.deviceid=ws.deviceid','LEFT')
            ->where('user_id',$user_id)
            ->where('status','normal')
            ->field('uw.*,ws.is_pay,ws.is_ka,ws.ka_endtime')
//            ->where('is_pay','in',[0,1])
            ->select();
        $this->success('获取绑定设备成功',$data);
    }

    //手表绑定
    public function bind(){
        $deviceid = $this->request->post('deviceid');
        $user_id = $this->auth->id;
        $notes = $this->request->post('notes');
//        $sex = $this->request->post('sex');
        $nickname = $this->request->post('nickname');
        $item = UserWatchModel::getWatch($user_id,$deviceid);
        $data = [
            'user_id'=>$user_id,
            'deviceid'=>$deviceid,
            'notes'=>$notes,
            'nickname'=>$nickname,
//            'sex'=>$sex,
            'status'=>'normal'
        ];
        //查询设备记录表是否录入
        $watchItem = WatchSetting::where(array('deviceid'=>$deviceid))->where('deletetime','NULL')->find();
        if(empty($watchItem)){
            $this->error('设备号错误,请联系管理员录入');
        }
        if(empty($item)){//绑定
            //查询该用户绑定数量
            $num = UserWatchModel::where(array('user_id'=>$user_id,'status'=>'normal'))->count();
            if($num>=10){
                $this->error('最多绑定10台设备');
            }
            $res = UserWatchModel::create($data);
            $id = $res->id;
        }else{
            UserWatchModel::where('id',$item['id'])->update($data);
            $id = $item['id'];
        }
        //give msg mobile
        //\app\common\library\Sms::send(\app\common\model\User::where('id',$user_id)->find()['mobile'], mt_rand(100000, 999999), 'notice');
        $resdata['id'] = $id;
        $this->success('绑定成功',$resdata);
    }

    //解绑手表
    public function unbind(){
        $id = $this->request->post('id');
        $item = UserWatchModel::get(array('id'=>$id));
        if(empty($item)){//绑定
            $this->error('未找到该设备');
        }else{
            if($item['status']=='normal'){

                UserWatchModel::where('id',$id)->update(array('status'=>'hidden'));
                $this->success('解绑成功');
            }else{
                $this->error('已解绑,不可重复操作');
            }
        }
    }

    //查询最新的一条数据
    public function last(){
        $deviceid = $this->request->post('deviceid');
        $WatchTurnoverModel = new WatchTurnover();
        $WatchHeartRateModel = new WatchHeartRate();
        $WatchBloodOxygenModel = new WatchBloodOxygen();
        $WatchBloodPressureModel = new WatchBloodPressure();
        //反转次数
        $data['turnover'] = $WatchTurnoverModel->order('id','desc')->where(array('deviceid'=>$deviceid))->find();
        //心率
        $data['heartRate'] = $WatchHeartRateModel->order('id','desc')->where(array('deviceid'=>$deviceid))->find();
        //血氧
        $data['oxygen'] = $WatchBloodOxygenModel->order('id','desc')->where(array('deviceid'=>$deviceid))->find();
        //血压
        $data['bloodpressure'] = $WatchBloodPressureModel->order('id','desc')->where(array('deviceid'=>$deviceid))->find();
        $data['sportstep'] = \app\common\model\Sportstep::order('createtime','desc')->where(array('deviceid'=>$deviceid))->find();
        $this->success('获取最新数据成功', $data);

    }

    //查询心率日/周/月/年的变化值
    public function heart(){
        $deviceid = $this->request->post('deviceid');
        $type = $this->request->post('type');//1日,2周,3月,4年
        $offset = $this->request->post('offset',0);//0当前周期,1上一个周期
        $WatchHeartRateModel = new WatchHeartRate();
        //心率
        //今天
        if($type == 1){
            $starttime = Date::unixtime('day', $offset, 'begin');
            $endtime = Date::unixtime('day', $offset, 'end');
            $data['list'] = $WatchHeartRateModel
                ->where('deviceid',$deviceid)
                ->where('createtime', 'between time', [$starttime, $endtime])
                ->select();
        }elseif($type == 2){
            $starttime = Date::unixtime('week', $offset, 'begin');
            $endtime = Date::unixtime('week', $offset, 'end');
            $data['list'] = $WatchHeartRateModel
                ->where('deviceid',$deviceid)
                ->where('createtime', 'between time', [$starttime, $endtime])
                ->group("DATE_FORMAT(FROM_UNIXTIME(createtime),'%Y-%m-%d')")
                ->field("id,createtime,min(value) min,max(value) max")
                ->select();//所有的心率变化
        }elseif ($type == 3){
            $starttime = Date::unixtime('month', $offset, 'begin');
            $endtime = Date::unixtime('month', $offset, 'end');
            $data['list'] = $WatchHeartRateModel
                ->where('deviceid',$deviceid)
                ->where('createtime', 'between time', [$starttime, $endtime])
                ->group("DATE_FORMAT(FROM_UNIXTIME(createtime),'%Y-%m-%d')")
                ->field("id,createtime,min(value) min,max(value) max")
                ->select();//所有的心率变化
        }else{
            $starttime = Date::unixtime('year', $offset, 'begin');
            $endtime = Date::unixtime('year', $offset, 'end');
            $data['list'] = $WatchHeartRateModel
                ->where('deviceid',$deviceid)
                ->where('createtime', 'between time', [$starttime, $endtime])
                ->group("DATE_FORMAT(FROM_UNIXTIME(createtime),'%Y-%m')")
                ->field("id,createtime,min(value) min,max(value) max")
                ->select();//所有的心率变化
        }
        $this->success('获取心率数据成功', $data);
    }

    //查询血氧日/周/月/年的变化值
    public function oxygen(){
        $deviceid = $this->request->post('deviceid');
        $type = $this->request->post('type');//1日,2周,3月,4年
        $offset = $this->request->post('offset',0);//0当前周期,1上一个周期
        $WatchBloodOxygenModel = new WatchBloodOxygen();
        //今天
        if($type == 1){
            $starttime = Date::unixtime('day', $offset, 'begin');
            $endtime = Date::unixtime('day', $offset, 'end');
            $data['list'] = $WatchBloodOxygenModel
                ->where('deviceid',$deviceid)
                ->where('createtime', 'between time', [$starttime, $endtime])
                ->select();
        }elseif($type == 2){
            $starttime = Date::unixtime('week', $offset, 'begin');
            $endtime = Date::unixtime('week', $offset, 'end');
            $data['list'] = $WatchBloodOxygenModel
                ->where('deviceid',$deviceid)
                ->where('createtime', 'between time', [$starttime, $endtime])
                ->group("DATE_FORMAT(FROM_UNIXTIME(createtime),'%Y-%m-%d')")
                ->field("id,createtime,min(value) min,max(value) max")
                ->select();
        }elseif ($type == 3){
            $starttime = Date::unixtime('month', $offset, 'begin');
            $endtime = Date::unixtime('month', $offset, 'end');
            $data['list'] = $WatchBloodOxygenModel
                ->where('deviceid',$deviceid)
                ->where('createtime', 'between time', [$starttime, $endtime])
                ->group("DATE_FORMAT(FROM_UNIXTIME(createtime),'%Y-%m-%d')")
                ->field("id,createtime,min(value) min,max(value) max")
                ->select();
        }else{
            $starttime = Date::unixtime('year', $offset, 'begin');
            $endtime = Date::unixtime('year', $offset, 'end');
            $data['list'] = $WatchBloodOxygenModel
                ->where('deviceid',$deviceid)
                ->where('createtime', 'between time', [$starttime, $endtime])
                ->group("DATE_FORMAT(FROM_UNIXTIME(createtime),'%Y-%m')")
                ->field("id,createtime,min(value) min,max(value) max")
                ->select();
        }
        $this->success('获取血氧数据成功', $data);
    }

    //查询血压日/周/月/年的变化值
    public function pressure(){
        $deviceid = $this->request->post('deviceid');
        $type = $this->request->post('type');//1日,2周,3月,4年
        $offset = $this->request->post('offset',0);//0当前周期,1上一个周期
        $WatchBloodPressureModel = new WatchBloodPressure();
        //今天
        if($type == 1){
            $starttime = Date::unixtime('day', $offset, 'begin');
            $endtime = Date::unixtime('day', $offset, 'end');
            $data['list'] = $WatchBloodPressureModel
                ->where('deviceid',$deviceid)
                ->where('createtime', 'between time', [$starttime, $endtime])
                ->select();
        }elseif($type == 2){
            $starttime = Date::unixtime('week', $offset, 'begin');
            $endtime = Date::unixtime('week', $offset, 'end');
            $data['list'] = $WatchBloodPressureModel
                ->where('deviceid',$deviceid)
                ->where('createtime', 'between time', [$starttime, $endtime])
                ->group("DATE_FORMAT(FROM_UNIXTIME(createtime),'%Y-%m-%d')")
                ->field("id,createtime,min(min_value) min,max(max_value) max")
                ->select();
        }elseif ($type == 3){
            $starttime = Date::unixtime('month', $offset, 'begin');
            $endtime = Date::unixtime('month', $offset, 'end');
            $data['list'] = $WatchBloodPressureModel
                ->where('deviceid',$deviceid)
                ->where('createtime', 'between time', [$starttime, $endtime])
                ->group("DATE_FORMAT(FROM_UNIXTIME(createtime),'%Y-%m-%d')")
                ->field("id,createtime,min(min_value) min,max(max_value) max")
                ->select();
        }else{
            $starttime = Date::unixtime('year', $offset, 'begin');
            $endtime = Date::unixtime('year', $offset, 'end');
            $data['list'] = $WatchBloodPressureModel
                ->where('deviceid',$deviceid)
                ->where('createtime', 'between time', [$starttime, $endtime])
                ->group("DATE_FORMAT(FROM_UNIXTIME(createtime),'%Y-%m')")
                ->field("id,createtime,min(min_value) min,max(max_value) max")
                ->select();
        }
        $this->success('获取血压数据成功', $data);
    }

    //sport step
    public function sportstep(){
        $deviceid = $this->request->post('deviceid');
        $type = $this->request->post('type');//1
        $offset = $this->request->post('offset',0);//0
        //
        //
        if($type == 1){
            $starttime = Date::unixtime('day', $offset, 'begin');
            $endtime = Date::unixtime('day', $offset, 'end');
            $data['list'] = \app\common\model\Sportstep::where('deviceid',$deviceid)
                ->where('createtime', 'between time', [$starttime, $endtime])
                ->select();
        }elseif($type == 2){
            $starttime = Date::unixtime('week', $offset, 'begin');
            $endtime = Date::unixtime('week', $offset, 'end');
            $data['list'] = \app\common\model\Sportstep::where('deviceid',$deviceid)
                ->where('createtime', 'between time', [$starttime, $endtime])
                ->group("DATE_FORMAT(FROM_UNIXTIME(createtime),'%Y-%m-%d')")
                ->field("id,createtime,min(value) min,max(value) max")
                ->select();//
        }elseif ($type == 3){
            $starttime = Date::unixtime('month', $offset, 'begin');
            $endtime = Date::unixtime('month', $offset, 'end');
            $data['list'] = \app\common\model\Sportstep::where('deviceid',$deviceid)
                ->where('createtime', 'between time', [$starttime, $endtime])
                ->group("DATE_FORMAT(FROM_UNIXTIME(createtime),'%Y-%m-%d')")
                ->field("id,createtime,min(value) min,max(value) max")
                ->select();//
        }else{
            $starttime = Date::unixtime('year', $offset, 'begin');
            $endtime = Date::unixtime('year', $offset, 'end');
            $data['list'] = \app\common\model\Sportstep::where('deviceid',$deviceid)
                ->where('createtime', 'between time', [$starttime, $endtime])
                ->group("DATE_FORMAT(FROM_UNIXTIME(createtime),'%Y-%m')")
                ->field("id,createtime,min(value) min,max(value) max")
                ->select();//
        }
        $this->success('获取步数数据成功', $data);
    }

}
